<?php
namespace app\index\controller;
use think\Controller;
class Base extends Controller 
{
	public $config;  //声明一个config变量来接收$confRes  
	
	public function _initialize(){
		$this->_getFooterArts();//获取并分配底部的帮助信息
		$this->_getNav();  //获取分配导航
		$this->_getConfs(); //获取并分配配置项
		$this->_getCates();
	}
	public function _getCates(){
		
		$cateRes=model('Category')->getCates();
		
		$this->assign([
			'cateRes'=>$cateRes,
		]);
	}
	
	
    public function _getFooterArts()
    {
		$mArticle=model('Article');
		if(cache('helpCateRes')){
			$helpCateRes = cache('helpCateRes');
		}else{
			$helpCateRes=$mArticle->getFooterArts();
			if($this->config['cache'] == '是'){
				cache('helpCateRes',$helpCateRes,$this->config['cache_time']);
			}
		}
		if(cache('shopInfoRes')){
			$shopInfoRes = cache('shopInfoRes');
		}else{
			$shopInfoRes = $mArticle->getShopInfo();
			if($this->config['cache'] == '是'){
				cache('shopInfoRes',$shopInfoRes,$this->config['cache_time']);
			}
		}
		//初始化获取article的数据库数据
		$this->assign([
			'helpCateRes' => $helpCateRes,
			'shopInfoRes'=>$shopInfoRes,
		]);
	}
	public function _getNav(){
		if(cache('navRes')){
			$navRes = cache('navRes');
		}else{
			$_navRes = db('nav')->select();
			$navRes = array();
			foreach($_navRes as $k=>$v){
				$navRes[$v['pos']][]=$v;  //把pos字段作为数组下标 [] 取值也是为pos字段值的$v 
			}
			cache('navRes',$navRes,3600);
		}
		$this->assign([
			'navRes'=>$navRes,
		]);
	}
	private function _getConfs(){
		if(cache('cofRes')){
			$cofRes = cache('confRes');
		}else{
			$confRes = model('Conf')->getConfs();
			cache('confRes',$confRes,3600);
		}
		$this->config = $confRes;
		$this->assign([
			'configs'=>$confRes,
		]);
	}
}
